﻿using FictionsDownload.BLL;
using FictionsDownload.Enums;
using FictionsDownload.Events;
using Zzw;
using FictionsDownload.Model;
using FictionsDownload.Static;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static FictionsDownload.Static.FormEx;

namespace FictionsDownload.UI
{
    public partial class FormReadTextBox : Form
    {
        #region 内部变量
        private BookModel bookModel = new BookModel();
        private Book book = new Book();
        private Directory directory = new Directory();
        #endregion
        #region 公共属性_阅读样式的配置
        public Font TextFont
        {
            get
            {
                return _textFont;
            }
            set
            {
                _textFont = value;
                this.SetFont(HtmlIdEnum.text, _textFont);
            }
        }
        private Font _textFont = new Font(FontFamily.GenericMonospace, 20, FontStyle.Regular);
        /// <summary>
        /// 顺序：Top Right Button Left
        /// 格式：10px 100px 10px 100px
        /// </summary>
        public string TextPadding
        {
            get
            {
                return _textPadding;
            }
            set
            {
                _textPadding = value;
                this.SetPadding(HtmlIdEnum.text, _textPadding);
            }
        }
        private string _textPadding;
        public Color BackgroundColor
        {
            get
            {
                return backgroundColor_;
            }
            set
            {
                backgroundColor_ = value;
                this.SetBackgroundColor(HtmlIdEnum.body, backgroundColor_);
            }
        }
        private Color backgroundColor_;
        public Color TextColor
        {
            get
            {
                return textColor_;
            }
            set
            {
                textColor_ = value;
                this.SetTextColor(HtmlIdEnum.text, value);
            }
        }
        private Color textColor_;
        #endregion
        #region 公共属性和事件
        /// <summary>
        /// 选中章节
        /// </summary>
        public event EventHandler<SelectionDirectoryEventArgs> SelectionDirectory;
        /// <summary>
        /// 下一章
        /// </summary>
        public event EventHandler NextDirectory;
        /// <summary>
        /// 上一章
        /// </summary>
        public event EventHandler PrevDirectory;
        #endregion
        #region 单例初始化
        private FormReadTextBox()
        {
            InitializeComponent();
            //
            this.TopLevel = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Show();
            //
        }
        private static FormReadTextBox Instance;
        private static readonly object lockRoot = new object();
        /// <summary>
        /// 初始化实例
        /// </summary>
        public static FormReadTextBox GetIntance
        {
            get
            {
                if (Instance == null)
                {
                    lock (lockRoot)
                    {
                        if (Instance == null)
                        {
                            Instance = new FormReadTextBox();
                        }
                    }
                }
                return Instance;
            }
        }
        #endregion
        #region 控件初始化
        /// <summary>
        /// 窗口初始化之后，控件再做调整
        /// </summary>
        public void InitializeComponentCode()
        {
            web_TextInit();
        }
        private void web_TextInit()
        {
            this.web_Text.ScriptErrorsSuppressed = true;
            //this.web_Text.IsWebBrowserContextMenuEnabled = false;
            this.web_Text.WebBrowserShortcutsEnabled = false;
            this.web_Text.AllowWebBrowserDrop = false;
            string path = Application.StartupPath + @"\UI\FormReadView\FormReadText\ReadText.html";
            this.web_Text.Navigate(path);
        }
        #endregion
        /// <summary>
        /// 传入章节ID显示文本
        /// </summary>
        /// <param name="directoryId"></param>
        public void SetReadText(string directoryId)
        {
            DirectoryModel directoryModel = directory.Find<DirectoryModel>(directoryId);
            if (directoryModel.Read != true)
            {
                directoryModel.Read = true;
                directory.Update(directoryModel);
                directory.SaveChanges();
            }
            this.SetText(HtmlIdEnum.title, directoryModel.Name);
            this.SetText(HtmlIdEnum.text, directoryModel.Text);
            //滚动条回到第一行
            this.web_Text.Document.Window.ScrollTo(0, 0);
        }
        private void FormReadTextBox_Load(object sender, EventArgs e)
        {
            web_Text.ObjectForScripting = this;
        }
        #region C#控制JS
        /// <summary>
        /// 修改标题和正文
        /// </summary>
        /// <param name="htmlId"></param>
        /// <param name="text"></param>
        public void SetText(HtmlIdEnum htmlId, string text)
        {
            web_Text.Document.InvokeScript("SetText",
                new object[] {
                    htmlId.ToString(),
                    text
                });
        }
        private void SetFont(HtmlIdEnum htmlId, Font font)
        {
            web_Text.Document.InvokeScript("SetFont",
                new object[] {
                    htmlId.ToString(),
                    font,
                });
        }
        private void SetBackground(HtmlIdEnum htmlId,Color color)
        {
            web_Text.Document.InvokeScript("SetBackground",
                new object[] {
                    htmlId.ToString(),
                    color.Name,
                });
        }
        private void SetPadding(HtmlIdEnum htmlId, string padding)
        {
            web_Text.Document.InvokeScript("SetPadding",
                new object[] {
                    htmlId.ToString(),
                    padding,
                });
        }
        private void SetBackgroundColor(HtmlIdEnum htmlId, Color color)
        {
            web_Text.Document.InvokeScript("SetBackgroundColor",
                new object[] {
                    htmlId.ToString(),
                    color.ToHtmlColor(),
                });
        }
        private void SetTextColor(HtmlIdEnum htmlId, Color color)
        {
            web_Text.Document.InvokeScript("SetTextColor",
                new object[] {
                    htmlId.ToString(),
                    color.ToHtmlColor(),
                });
        }
        #endregion
        #region JS调用C#
        public void web_Text_Init()
        {
            this.SetFont(HtmlIdEnum.title, new Font(FontFamily.GenericMonospace, 30, FontStyle.Bold));
            this.SetFont(HtmlIdEnum.text, TextFont);
            this.SetPadding(HtmlIdEnum.title, "20px");
            this.SetPadding(HtmlIdEnum.text, TextPadding);
            this.SetBackgroundColor(HtmlIdEnum.body, BackgroundColor);
            this.SetTextColor(HtmlIdEnum.text, TextColor);
        }
        public void PrevDirectoryGo()
        {
            this.PrevDirectory?.Invoke(this, null);
        }
        public void ReturnDirectoryGo()
        {
            this.Parent.Parent.Parent.Visible = false;
        }
        public void NextDirectoryGo()
        {
            this.NextDirectory?.Invoke(this, null);
        }
        #endregion

        private void web_Text_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Left:
                    PrevDirectoryGo();
                    break;
                case Keys.Right:
                    NextDirectoryGo();
                    break;
                case Keys.Enter:
                    ReturnDirectoryGo();
                    break;
            }
        }
    }
}
